<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:template match="field[@type = 'multi_link' and parent::doctype]">
		<xsl:param name="document_field" select="../../document[@doctype_name = current()/parent::doctype/@name]/field[@name = current()/@name]"/>
		<xsl:variable name="is_new" select="parent::doctype/@action = 'create'"/>
		<xsl:variable name="item_show_count">1</xsl:variable>
		<script type="text/javascript" src="{$static_prefix}/js/multi_link_dtf.js"/>
		<xsl:call-template name="one_item">
			<xsl:with-param name="document_field" select="$document_field"/>
			<xsl:with-param name="max_item_count" select="@max_item_count"/>
			<xsl:with-param name="current_count" select="1"/>
		</xsl:call-template>
		<div class="field" id="multi_link_dtf_add_button_{@name}">
			<span style="border-bottom: 1px dotted #000000; cursor: pointer;" onclick="multi_link_dtf_show_item('{@name}')">
				<xsl:choose>
					<xsl:when test="@add_item_text != ''">
						<xsl:value-of select="@add_item_text" />
					</xsl:when>
					<xsl:otherwise>
						<xsl:apply-templates mode="dtf_multi_link_add_more" select="." />
					</xsl:otherwise>
				</xsl:choose>
			</span>
			<br/>
		</div>
		<input type="hidden" id="multi_link_dtf_shown_count_{@name}" name="multi_link_dtf_shown_count_{@name}" value="{@max_item_count}"/>
		<script type="text/javascript">
			multi_link_dtf_set_shown_count('<xsl:value-of select="@name"/>', '1');
		</script>
	</xsl:template>
	<xsl:template name="one_item">
		<xsl:param name="document_field"/>
		<xsl:param name="max_item_count"/>
		<xsl:param name="current_count"/>
		<div class="field dn" id="multi_link_item_{@name}_{$current_count}" style="">
			<xsl:if test="$current_count = 1 or $document_field/item[$current_count]/@id">
				<xsl:attribute name="class">field</xsl:attribute>
				<script type="text/javascript">
					multi_link_dtf_set_shown_count('<xsl:value-of select="@name"/>', '<xsl:value-of select="$current_count"/>');
				</script>
			</xsl:if>
			<xsl:if test="not($document_field/item[$current_count]/@id)">
				<script type="text/javascript">
					multi_link_dtf_add_item('<xsl:value-of select="@name"/>', 'multi_link_item_<xsl:value-of select="@name"/>_<xsl:value-of select="$current_count"/>', '<xsl:value-of select="$current_count"/>');
				</script>
			</xsl:if>
			<label class="title_label" for="{@name}">
				<xsl:value-of select="@title"/>
				<xsl:text>:&#160;</xsl:text>
				<xsl:if test="@is_important = 1">
					<span class="star">*</span>
				</xsl:if>
			</label>
			<select name="{@name}[]">
				<option value="">
					<xsl:apply-templates mode="dtf_multi_link_not_selected" select="." />
				</option>
				<xsl:for-each select="item">
					<option value="{@value}">
						<xsl:choose>
							<xsl:when test="../../../../pass_info/vars/var[@name = current()/../../@name] = @value">
								<xsl:attribute name="selected">selected</xsl:attribute>
							</xsl:when>
							<xsl:when test="../../../../pass_info/vars/var[@name = current()/../../@name]"/>
							<xsl:when test="$document_field/item[$current_count]/@id = @value">
								<xsl:attribute name="selected">selected</xsl:attribute>
							</xsl:when>
						</xsl:choose>
						<xsl:value-of select="@title"/>
					</option>
				</xsl:for-each>
			</select>
			<xsl:apply-templates mode="dtf_error" select="." />
			<xsl:if test="@comment != ''">
				<div class="comment-">
					<xsl:value-of select="@comment"/>
				</div>
			</xsl:if>
		</div>
		<xsl:if test="$max_item_count > $current_count">
			<xsl:call-template name="one_item">
				<xsl:with-param name="document_field" select="$document_field"/>
				<xsl:with-param name="max_item_count" select="$max_item_count"/>
				<xsl:with-param name="current_count" select="$current_count+1"/>
			</xsl:call-template>
		</xsl:if>
	</xsl:template>
</xsl:stylesheet>
